﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for InstructorManagementWindow.xaml
    /// </summary>
    public partial class InstructorManagementWindow : Window
    {
        public InstructorManagementWindow()
        {
            InitializeComponent();
            loadAllWorkers();
            updateInstructorChangesButton.IsEnabled = false;
            removeInstructorButton.IsEnabled = false;
        }

        private void newInstructorButton_Click(object sender, RoutedEventArgs e)
        {
            GymEmployeeCreationWindow gymEmployeeCreationWindow = new GymEmployeeCreationWindow();
            gymEmployeeCreationWindow.ShowDialog();
            loadAllWorkers();
        }

        private void loadAllWorkers()
        {
            //instructorsListBox.SelectedIndex = -1;
            instructorsListBox.DisplayMemberPath = "FullName";
            //try
            //{
                instructorsListBox.ItemsSource = GymWorkerManager.Instance.GetWorkers(false);
            //}
            //catch (Exception ex)
            //{
                //System.Console.WriteLine(ex.StackTrace.ToString());
            //}
        }

        private void instructorsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (instructorsListBox.SelectedItem != null)
            {
                GymWorker selected = instructorsListBox.SelectedItem as GymWorker;
                textBoxFirstName.Text = selected.FirstName;
                textBoxLastName.Text = selected.LastName;
                mailTextBox.Text = selected.Email;
                phoneTextBox.Text = selected.Phone;
                isAdmin.IsChecked = selected.IsAdminitrator;
                userName.Text = selected.UserName;
                updateInstructorChangesButton.IsEnabled = true;
                removeInstructorButton.IsEnabled = true;
                textBoxFirstName.Background = Brushes.White;
                textBoxLastName.Background = Brushes.White;
                mailTextBox.Background = Brushes.White;
                phoneTextBox.Background = Brushes.White;
                errorLabel.Content = String.Empty;
            }
            else
            {
                updateInstructorChangesButton.IsEnabled = false;
                removeInstructorButton.IsEnabled = false;
            }
        }

        private void updateInstructorChangesButton_Click(object sender, RoutedEventArgs e)
        {
            GymWorker selected = instructorsListBox.SelectedItem as GymWorker;
            
            if (validateForm() == true)
            {
                selected.FirstName = textBoxFirstName.Text;
                selected.LastName = textBoxLastName.Text;
                selected.Email = mailTextBox.Text;
                selected.Phone = phoneTextBox.Text;
                selected.IsAdminitrator = isAdmin.IsChecked.Value;
                GymWorkerManager.Instance.UpdateWorker(selected);
                loadAllWorkers();
            }
        }

        private void removeInstructorButton_Click(object sender, RoutedEventArgs e)
        {
            GymWorker selected = instructorsListBox.SelectedItem as GymWorker;
            if (GymWorkerManager.Instance.DeleteWorker(selected) == true)
            {
                loadAllWorkers();
            }
        }

        private bool validateForm()
        {
            bool isValid = true;

            textBoxFirstName.Background = Brushes.White;
            textBoxLastName.Background = Brushes.White;
            mailTextBox.Background = Brushes.White;
            phoneTextBox.Background = Brushes.White;
            errorLabel.Content = String.Empty;

            if (textBoxFirstName.Text.Equals(""))
            {
                isValid = false;
                textBoxFirstName.Background = Brushes.Pink;
                errorLabel.Content = "Please Fill First Name";
            }

            if (textBoxLastName.Text.Equals(""))
            {
                isValid = false;
                textBoxLastName.Background = Brushes.Pink;
                errorLabel.Content = "Please Fill Last Name";
            }

            if (mailTextBox.Text.Equals("") || EmailService.IsValidEmail(mailTextBox.Text) == false)
            {
                isValid = false;
                mailTextBox.Background = Brushes.Pink;
                errorLabel.Content = "Please Fill Valid Email";
            }

            if (phoneTextBox.Text.Equals(""))
            {
                isValid = false;
                phoneTextBox.Background = Brushes.Pink;
                errorLabel.Content = "Please Fill Phone Number";
            }

            return isValid;
        }
    }
}
